"""The ListPanel module contains the ListPanel class, which builds the GUI for the item list"""

import wx
import sys

class ListPanel(wx.Panel):
    def __init__(self, parent, id, plgen):
        self.plgen = plgen

        wx.Panel.__init__(self, parent, wx.ID_ANY)
        
        self.createList(self)

        lpsizer = wx.BoxSizer()
        lpsizer.Add(self.list, 1, wx.EXPAND)
        self.SetSizer(lpsizer)

    def createList(self, panel):
        self.list = wx.ListCtrl(panel, -1, style=wx.LC_REPORT)

        self.list.InsertColumn(0, "Filename")
        self.list.InsertColumn(1, "Tags")
        self.list.InsertColumn(2, "Rating")

        self.updateList()

        self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE_USEHEADER)
        self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE_USEHEADER)
        self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE_USEHEADER)

    def updateList(self):
        self.list.DeleteAllItems()

        itemlist = self.plgen.getCompleteList()

        for item in itemlist:
            index = self.list.InsertStringItem(sys.maxint, item['filename'])
            self.list.SetStringItem(index, 1, ", ".join(item['tags']))
            self.list.SetStringItem(index, 2, item["rating"])
        
        self.list.SetColumnWidth(0, wx.LIST_AUTOSIZE_USEHEADER)
        self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE_USEHEADER)
        self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE_USEHEADER)